﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Scalextric
{
    /// <summary>
    /// Interaction logic for F1RaceFlag.xaml
    /// </summary>
    public partial class F1RaceFlag : UserControl
    {
        private Flags _Flag = Flags.Default; //default
        public Flags Flag { get { return _Flag; } set { _Flag = value; SetFlag(_Flag); } }

        //event for flag changed?

        public F1RaceFlag()
        {
            InitializeComponent();
        }

        public void SetFlag(Flags newFlag)
        {
            //update flag to vew one
            ChangeFlag(newFlag);
            //raise event
        }

        public void ResetFlag()
        {
            ChangeFlag(Flags.Default);
        }

        private void ChangeFlag(Flags nFlag)
        {
            switch (nFlag)
            {
                case Flags.Danger:
                    //yellow flag solid
                    SetSolidFlag(this.Resources["Yellow1"] as SolidColorBrush);
                    break;
                case Flags.TrackClear:
                    //green solid
                    SetSolidFlag(this.Resources["Green1"] as SolidColorBrush);
                    break;
                case Flags.Suspended:
                    //red solid
                    SetSolidFlag(this.Resources["Red1"] as SolidColorBrush);
                    break;
                case Flags.Overtake:
                    //blue solid
                    SetSolidFlag(this.Resources["Blue1"] as SolidColorBrush);
                    break;
                case Flags.SlowMovingCar:
                    //white solid
                    SetSolidFlag(this.Resources["White1"] as SolidColorBrush);
                    break;
                case Flags.Disqualified:
                    //black solid
                    SetSolidFlag(this.Resources["Black1"] as SolidColorBrush);
                    break;
                case Flags.EndofRace:
                    //chequred
                    SetChequeredFlag();
                    break;
                case Flags.UnsportingBehaviour:
                    //diagonal
                    SetDiagonalFlag();
                    break;
                case Flags.MechanicalProblem:
                    //dot orange
                    SetDotFlag();
                    break;
                case Flags.SlipperyTrack:
                    //red yellow stripes
                    SetLinearFlag();
                    break;
                case Flags.Default:
                    //grey flag - solid
                    SetSolidFlag(this.Resources["Grey1"] as SolidColorBrush);
                    break;
                default:
                    //grey flag - solid
                    SetSolidFlag(this.Resources["Grey1"] as SolidColorBrush);
                    break;
            }
        }

        private void SetSolidFlag(SolidColorBrush brush)
        {
            //hide all others
            SolidRectangle.Visibility = Visibility.Visible;
            CheqRectangle.Visibility = Visibility.Hidden;
            DiagonalRectangle.Visibility = Visibility.Hidden;
            LinearRectangle.Visibility = Visibility.Hidden;
            DotRectangle.Visibility = Visibility.Hidden;
            //set colour
            SolidRectangle.Fill = brush;
        }

        private void SetChequeredFlag()
        {
            SolidRectangle.Visibility = Visibility.Hidden;
            CheqRectangle.Visibility = Visibility.Visible;
            DiagonalRectangle.Visibility = Visibility.Hidden;
            LinearRectangle.Visibility = Visibility.Hidden;
            DotRectangle.Visibility = Visibility.Hidden;
        }

        private void SetLinearFlag()
        {
            SolidRectangle.Visibility = Visibility.Hidden;
            CheqRectangle.Visibility = Visibility.Hidden;
            DiagonalRectangle.Visibility = Visibility.Hidden;
            LinearRectangle.Visibility = Visibility.Visible;
            DotRectangle.Visibility = Visibility.Hidden;
        }

        private void SetDiagonalFlag()
        {
            SolidRectangle.Visibility = Visibility.Hidden;
            CheqRectangle.Visibility = Visibility.Hidden;
            DiagonalRectangle.Visibility = Visibility.Visible;
            LinearRectangle.Visibility = Visibility.Hidden;
            DotRectangle.Visibility = Visibility.Hidden;
        }

        private void SetDotFlag()
        {
            SolidRectangle.Visibility = Visibility.Hidden;
            CheqRectangle.Visibility = Visibility.Hidden;
            DiagonalRectangle.Visibility = Visibility.Hidden;
            LinearRectangle.Visibility = Visibility.Hidden;
            DotRectangle.Visibility = Visibility.Visible;
        }
    }

    public enum Flags
    {
        Danger,
        TrackClear,
        Suspended,
        Overtake,
        SlowMovingCar,
        Disqualified,
        EndofRace,
        UnsportingBehaviour,
        MechanicalProblem,
        SlipperyTrack,
        Default
    }
}
